package com.freecat.xmqgct.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.freecat.xmqgct.MainActivity;
import com.freecat.xmqgct.R;
import com.freecat.xmqgct.app.MyApp;
import com.freecat.xmqgct.mvp.model.entity.AccountBean;
import com.freecat.xmqgct.mvp.model.entity.LoginBean;
import com.freecat.xmqgct.mvp.presenter.LoginPresenter;
import com.freecat.xmqgct.mvp.ui.adapter.AutoAdapter;
import com.freecat.xmqgct.utils.Constant;
import com.freecat.xmqgct.utils.Tools;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.objectbox.Box;
import me.jessyan.art.mvp.IView;
import me.jessyan.art.mvp.Message;
import me.jessyan.art.utils.ArtUtils;


/**
 * ================================================
 * Description:
 * 账号登录
 * ================================================
 *
 * @author ChenPeng
 */
public class AccountLoginActivity extends BaseActivity<LoginPresenter> implements IView {

//    @BindView(R.id.iv_back)
//    ImageView ivBack;
    @BindView(R.id.et_account)
    AutoCompleteTextView etAccount;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.tv_forget_password)
    TextView tvForgetPassword;

    @BindView(R.id.tv_account_reg)
    TextView tv_account_reg;

    @BindView(R.id.tv_account_lw_login)
    TextView tv_account_lw_login;

    @BindView(R.id.tv_mian_yzm)
    TextView tv_mian_yzm;


    @BindView(R.id.et_main_yzm)
    TextView et_main_yzm;


    @BindView(R.id.tv_mian_zh)
    TextView tv_mian_zh;

    @BindView(R.id.tv_main_mm)
    TextView tv_main_mm;


    int login_type=1;

    String yzm;

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_account_login;
    }


    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        String account = SPUtils.getInstance(Constant.SP_CONFIG).getString(Constant.ACCOUNT);
        String password = SPUtils.getInstance(Constant.SP_CONFIG).getString(Constant.PASSWORD);

        String yzm1 = SPUtils.getInstance(Constant.SP_CONFIG).getString(Constant.lw_yzm);
        if (password!=null&&password.length()>3){

            etPassword.setText(password);
        }
        if (account!=null&&password.length()>3){
            etAccount.setText(account);

        }
        if (yzm1!=null&&yzm1.length()>3){
            et_main_yzm.setText(yzm1);
        }



        setListener();
    }


    private void setListener() {
        List<AccountBean> beans = MyApp.getBoxStore().boxFor(AccountBean.class).getAll();
        if (beans != null && beans.size() > 0) {
            AutoAdapter adapter = new AutoAdapter(this, beans);
            etAccount.setAdapter(adapter);
//            etAccount.setOnFocusChangeListener(new View.OnFocusChangeListener() {
//                @Override
//                public void onFocusChange(View v, boolean hasFocus) {
//                    if (hasFocus) {//获取焦点时
//                        try {
//                            etAccount.showDropDown();
//                        }catch (Exception e){
//                            e.printStackTrace();
//                        }
//                    }
//                }
//            });
        }
    }


    @Override
    @Nullable
    public LoginPresenter obtainPresenter() {
        return new LoginPresenter(ArtUtils.obtainAppComponentFromContext(this));
    }

    @Override
    public void showLoading() {
        progressDialog.show();
    }

    @Override
    public void hideLoading() {
        progressDialog.dismiss();
    }

    @Override
    public void showMessage(@NonNull String message) {

        ArtUtils.makeText(this, message);
    }

    @Override
    public void handleMessage(@NonNull Message message) {

        switch (message.what) {
            case LoginPresenter.LOGIN_SUCCESS:
                System.out.println("登录成功-开始跳转");
                LoginBean loginBean = (LoginBean) message.obj;
                System.out.println("message.obj=="+message.obj.toString());

                if (loginBean != null) {
//                    System.out.println("loginBean.getToken()"+loginBean.getToken());
//                    System.out.println("loginBean.getRybh()"+loginBean.getRybh());
//                    System.out.println("loginBean.getUsername()"+loginBean.getUsername());
//                    System.out.println("loginBean.getRyxm()"+loginBean.getRyxm());
//                    System.out.println("loginBean.getDwmc()"+loginBean.getDwmc());
//                    System.out.println("loginBean.getZhlx()"+loginBean.getZhlx());
//                    System.out.println("loginBean.getRytx()"+loginBean.getRytx());
//                    System.out.println("loginBean.getCslist()"+loginBean.getCslist().toString());

                    SPUtils spUtils = SPUtils.getInstance(Constant.SP_CONFIG);

                    long totalMilliSeconds = System.currentTimeMillis();
                    long totalSeconds = totalMilliSeconds / 1000;

                    spUtils.remove(Constant.Time_Token);
                    spUtils.put(Constant.Time_Token, String.valueOf(totalSeconds));
                    spUtils.put(Constant.TOKEN, loginBean.getToken());

//                    spUtils.put(Constant.TOKEN, "53bdc6a1-07c4-4aed-a231-d058499a8031");

                    spUtils.put(Constant.USER_ID, loginBean.getRybh());
                    spUtils.put(Constant.ACCOUNT, loginBean.getUsername());
                    spUtils.put(Constant.PASSWORD, etPassword.getText().toString());
                    spUtils.put(Constant.USER_NAME, loginBean.getRyxm());
                    spUtils.put(Constant.UNIT_NAME, loginBean.getDwmc());
                    spUtils.put(Constant.ZH_LX, loginBean.getZhlx());
                    spUtils.put(Constant.USER_AVATAR, loginBean.getRytx());

                    List<LoginBean.CslistBean> cslist = loginBean.getCslist();
                    System.out.println("cslist=============="+cslist.size());

                    for (LoginBean.CslistBean cslistBean : cslist) {

                        switch (cslistBean.getCsmc()) {
                            case "WDSWSC":
                                System.out.println("WDSWSC");
                                spUtils.put(Constant.SWSJ, cslistBean.getCsz());
                                break;
                            case "ZKJLCS":
                                System.out.println("ZKJLCS");
                                spUtils.put(Constant.ZKJL, cslistBean.getCsz());
                                break;
                            case "RLSBSJCS":
                                System.out.println("RLSBSJCS");
                                spUtils.put(Constant.RLSBSJCS, cslistBean.getCsz());
                                break;
                            case "ZKWZJLCS":
                                System.out.println("ZKWZJLCS");
                                spUtils.put(Constant.ZKWZJLCS, cslistBean.getCsz());
                                break;
                        }
                    }

//                    String account = loginBean.getUsername();
//                    Box<AccountBean> box = MyApp.getBoxStore().boxFor(AccountBean.class);
//                    AccountBean unique = box.query().equal(AccountBean_.account, account).build().findUnique();
//                    if (unique == null) {
//                        AccountBean accountBean = new AccountBean(account);
//                        box.put(accountBean);
//                    }
//                    ArtUtils.startActivity(MainActivity.class);
//                    finish();
                }
                break;
            case 2:
                break;

            case LoginPresenter.LW_LOGIN_SUCCESS:


                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btn_login, R.id.tv_forget_password,R.id.tv_account_reg,R.id.tv_account_lw_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
//            case R.id.iv_back:
//                finish();
//                break;
            case R.id.btn_login:


                ArtUtils.startActivity(MainActivity.class);
//                if (login_type==2){
//
////                    if (NetworkUtils.isConnected()){
////                        lw_login();
////
////                    }
//
//                }else {
//
//                    String token = SPUtils.getInstance(Constant.SP_CONFIG).getString(Constant.TOKEN);
//                    boolean connected = NetworkUtils.isConnected();
//                    if (connected) {
//                        SPUtils.getInstance(Constant.SP_CONFIG).put(Constant.OFFLINE, false);
//                        login();
//                    } else {
//                        if (TextUtils.isEmpty(token)) {
//                            ToastUtils.showShort("第一次使用需在线登录");
//                        } else {
//                            SPUtils.getInstance(Constant.SP_CONFIG).put(Constant.OFFLINE, true);
//                            loginOffline();
//                        }
//                    }
//                }

                break;
            case R.id.tv_forget_password:
//                ArtUtils.startActivity(ForgetPasswordActivity.class);
                break;
            case R.id.tv_account_reg:
//                ArtUtils.startActivity(RegisterActivity.class);
                break;
            case R.id.tv_account_lw_login:
//                ArtUtils.startActivity(ForgetPasswordActivity.class);

                if (login_type==1){
                    tv_account_lw_login.setText("南江登录");
                    //验证码登录显示
                    et_main_yzm.setVisibility(View.VISIBLE);
                    tv_mian_yzm.setVisibility(View.VISIBLE);
                    tv_mian_zh.setVisibility(View.GONE);
                    etAccount.setVisibility(View.GONE);
                    tv_main_mm.setVisibility(View.GONE);
                    etPassword.setVisibility(View.GONE);
                    login_type=2;
                }else {

                    tv_account_lw_login.setText("劳务登录");
                    et_main_yzm.setVisibility(View.GONE);
                    tv_mian_yzm.setVisibility(View.GONE);
                    tv_mian_zh.setVisibility(View.VISIBLE);
                    etAccount.setVisibility(View.VISIBLE);
                    tv_main_mm.setVisibility(View.VISIBLE);
                    etPassword.setVisibility(View.VISIBLE);
                    login_type=1;
                }


                break;
        }
    }

    private void loginOffline() {
        String account = etAccount.getText().toString().trim();
        String password = etPassword.getText().toString().trim();
        String account_ = SPUtils.getInstance(Constant.SP_CONFIG).getString(Constant.ACCOUNT);
        String password_ = SPUtils.getInstance(Constant.SP_CONFIG).getString(Constant.PASSWORD);
        if (TextUtils.isEmpty(account)) {
            ToastUtils.showShort("请输入账号");
            return;
        } else if (TextUtils.isEmpty(password)) {
            ToastUtils.showShort("请输入密码");
            return;
        }
        if (account.equals(account_) && password.equals(password_)) {
            ArtUtils.startActivity(MainActivity.class);
            finish();
        } else {
            ToastUtils.showShort("账号密码错误");
        }
    }


    private void login() {
        String account = etAccount.getText().toString().trim();
        String password = etPassword.getText().toString().trim();
        if (TextUtils.isEmpty(account)) {
            ToastUtils.showShort("请输入账号");
            return;
        } else if (TextUtils.isEmpty(password)) {
            ToastUtils.showShort("请输入密码");
            return;
        }
        password= Tools.md5Password(password);

//        System.out.println("password=="+password);
//        mPresenter.login(Message.obtain(this, new String[]{account, password}));
    }


    private void lw_login() {
//        String account = etAccount.getText().toString().trim();
//        String password = etPassword.getText().toString().trim();
//        if (TextUtils.isEmpty(account)) {
//            ToastUtils.showShort("请输入账号");
//            return;
//        } else if (TextUtils.isEmpty(password)) {
//            ToastUtils.showShort("请输入密码");
//            return;
//        }
//        password= Tools.md5Password(password);


        yzm = et_main_yzm.getText().toString().trim();

        if (TextUtils.isEmpty(yzm)) {
            ToastUtils.showShort("请输入验证码");
            return;
        }

//        System.out.println("password=="+password);

        Map<String, String> map = new HashMap<>();
        map.put("yzm", yzm);

//        mPresenter.lw_login(Message.obtain(this, new Object[]{map}));
    }
}
